﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RDPCommon
{
    public partial class frmWaiting : Form
    {
        public frmWaiting()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmWaiting_Load);
            this.FormClosed += new FormClosedEventHandler(frmWaiting_FormClosed);
        }

        void frmWaiting_FormClosed(object sender, FormClosedEventArgs e)
        {
            th.Abort();
        }

        System.Threading.Thread th = null;
        void frmWaiting_Load(object sender, EventArgs e)
        {
            th = new System.Threading.Thread(new System.Threading.ThreadStart(Start));
            th.IsBackground = true;
            th.Start();
        }

        void Start()
        {
            int i = 0;
            while (true && frm != null)
            {
                i++;
                if (progressBar1.InvokeRequired)
                    progressBar1.BeginInvoke(new SetProgressDelegate(SetProgress), i);
                else
                    SetProgress(i);
                if (i == 10)
                    i = 0;
                Application.DoEvents();
                System.Threading.Thread.Sleep(50);
            }
        }

        delegate void SetProgressDelegate(int i);

        void SetProgress(int i)
        {
            progressBar1.Value = i*10;
            this.Refresh();
        }

        static frmWaiting frm;
        public static void ShowWaiting()
        {
            if (frm != null)
                return;
            frm = new frmWaiting();
            frm.TopMost = true;
            frm.progressBar1.Maximum = 100;
            frm.progressBar1.Minimum = 0;

            ShowFormDelegate xx = new ShowFormDelegate(frm.ShowForm);
            xx.BeginInvoke(null,null );
        }

        delegate DialogResult ShowFormDelegate();
        DialogResult ShowForm()
        {
            frm.Cursor = Cursors.WaitCursor;
            return frm.ShowDialog();
        }

        delegate void CloseFormDelegate();
        public static void CloseWaiting()
        {
            if (frm != null)
            {
                if (frm.InvokeRequired)
                    frm.Invoke(new CloseFormDelegate(frm.CloseForm));
                else
                    frm.Close();
            }
        }
        void CloseForm()
        {
            frm.Close();
            frm = null;
        }

        //拖动窗体
        internal static int WM_NCHITTEST = 0x84;
        internal static IntPtr HTCLIENT = (IntPtr)0x1;
        internal static IntPtr HTCAPTION = (IntPtr)0x2;
        internal static int WM_NCLBUTTONDBLCLK = 0x00A3;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCLBUTTONDBLCLK)
            {
                return;
            }
            if (m.Msg == WM_NCHITTEST)
            {
                base.WndProc(ref m);
                if (m.Result == HTCLIENT)
                {
                    m.HWnd = this.Handle;
                    m.Result = HTCAPTION;
                }
                return;
            }
            base.WndProc(ref m);            
        }
    }
}

